package com.ecdc.soexample;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ecdc.annotation.BindViewUtils;
import com.ecdc.annotation.EcdcAnnotation;
import com.deep_tools.ecdc.R;//import com.ecdc.touchelxcdds.R;
//import com.ecdc.touchelx.R;

import java.util.ArrayList;
import java.util.List;

import adapter.BluetoothDeviceAdapter;
import bluetooth.BluetoothLeService;
import bluetooth.GattUpdateReceiver;
import bluetooth.MyServiceConnection;
import bluetooth.SampleGattAttributes;
import bluetooth.Utils;
import utils.ToastUtils;

public class PairingWatchActivity extends BaseActivity2   {

    @EcdcAnnotation.EcdcAnno(R.id.pairing_activity_pairing_watch)
    private TextView pairing_activity_pairing_watch;

    // 服务
//    public static BluetoothLeService mBluetoothLeService;
//    public static BluetoothGattCharacteristic  bluetoothGattCharacteristicUUID_NOTIFY_NOTIFY, bluetoothGattCharacteristicUUID_NOTIFY_WRITE_NO_RESPONSE;
    private MyServiceConnection mMyServiceConnection;
    private  GattUpdateReceiver mGattUpdateReceiver;
    private int firstOpen;//是否是第一次打开软件
    private String bluetoothDeviceAddress;//蓝牙连接地址
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0://开始绑定服务
                    // 绑定服务
                     mMyServiceConnection= new MyServiceConnection(bluetoothDeviceAddress);
                    Intent gattServiceIntent = new Intent(PairingWatchActivity.this, BluetoothLeService.class);
                    bindService(gattServiceIntent, mMyServiceConnection, BIND_AUTO_CREATE);

                    break;
                case 1://蓝牙连接成功
//                    initBL();
                    Intent intent = new Intent(PairingWatchActivity.this, HomeActivity.class);
                    intent.putExtra("bluetoothDeviceAddress",bluetoothDeviceAddress);
                    startActivity(intent);
                    PairingWatchActivity.this.finish();

                    break;
                case 2:
//                    starOrStopScanBluetoothDevice(STOPSCAN);

                    break;
                case 3:
                    String content = pairing_activity_pairing_watch.getText().toString();
                    int oldLength = content.length();

                    if (oldLength == 0) {
                        pairing_activity_pairing_watch.setText(".");
                    } else if (oldLength == 1) {
                        pairing_activity_pairing_watch.setText("..");

                    } else if (oldLength == 2) {
                        pairing_activity_pairing_watch.setText("...");

                    } else if (oldLength == 3) {
                        pairing_activity_pairing_watch.setText("");

                    }
                    handler.sendEmptyMessageDelayed(3, 400);
                    break;
                default:
                    break;

            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pairing_watch);
        bluetoothDeviceAddress = getIntent().getStringExtra("bluetoothDeviceAddress");


        try {
            BindViewUtils.getInstance().initEcdcAnnotationActivity(this, this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        GattUpdateReceiver.mBluettothInterface=this;
         mGattUpdateReceiver=new GattUpdateReceiver();
        registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());// 开始广播

        handler.sendEmptyMessage(3);//3个点的动画效果
        handler.sendEmptyMessageDelayed(0, 1000);
        Toast.makeText(PairingWatchActivity.this, bluetoothDeviceAddress, Toast.LENGTH_SHORT).show();

        onClickL();

    }

    private void onClickL() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mMyServiceConnection);
        unregisterReceiver(mGattUpdateReceiver);
    }

    private IntentFilter makeGattUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); // 蓝牙状态广播
        intentFilter.addAction(BluetoothLeService.EXTRA_DATA_MTU); // 蓝牙状态广播
        return intentFilter;
    }

    @Override
    public void ddd() {
//        super.ddd();
//        this.finish();
        handler.sendEmptyMessage(1);

    }

    @Override
    public void getCurrentElectricQuantit(String electricQuantit) {
//        super.getCurrentElectricQuantit(electricQuantit);
        Toast.makeText(App.context, "peidui", Toast.LENGTH_SHORT).show();

    }
}
